﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using WCFMINERP.MODEL;
using WCFMINERP.COM;
using WCFMINERP.Order.Presenter;
using Leadtools.Codecs;
using System.IO;

namespace WCFMINERP.Order.Forms
{
    public partial class FrmOrderInfo : Form
    {
        private List<OrderInfo> _orderInfoList = null;//产品列表
        public OrderPresenter _presenter;
        public ShowType showtype;

        public FrmOrderInfo(List<OrderInfo> orderInfoList)
        {
            InitializeComponent();
            _orderInfoList = orderInfoList;
        }

        private void FrmOrderInfo_Load(object sender, EventArgs e)
        {
            LinkOrderInfo();
            LinkPhotoInfo();
            if (showtype == ShowType.addPhoto)
            {
                tsbtnAddPhotoToOrderInfo.Visible = false;

                tsbtnAddPhoto.Visible = true;
                tsbtnDeleteOrderInfo.Visible = true;
                tsbtnDeletePhoto.Visible = true;
            }
            else
            {
                tsbtnAddPhotoToOrderInfo.Visible = true;
                tsbtnAddPhoto.Visible = false;
                tsbtnDeleteOrderInfo.Visible = false;
                tsbtnDeletePhoto.Visible = false;
            }
        }


        /// <summary>
        /// 绑定产品列表
        /// </summary>
        private void LinkOrderInfo()
        {
            gcOrderInfo.DataSource = _presenter._orderInfoList;
            gvOrderInfo.RefreshData();
        }

        private void LinkPhotoInfo()
        {
            OrderInfo oi =gvOrderInfo.GetFocusedRow() as OrderInfo;
            if (oi != null)
            {
                gcPhotoInfo.DataSource = oi.PhotoList;
            }
            gcPhotoInfo.RefreshDataSource();
        }

        private void tssbtnAddPhotoToOrderInfo_Click(object sender, EventArgs e)
        {
            OrderInfo oi = gvOrderInfo.GetFocusedRow() as OrderInfo;
            _presenter.orderInfoSelectedToAddPhotos = oi;
            this.Close();

        }

        private void gvOrderInfo_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            LinkPhotoInfo();
        }

        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbtnDeleteOrderInfo_Click(object sender, EventArgs e)
        {
            OrderInfo oi = gvOrderInfo.GetFocusedRow() as OrderInfo;
            _presenter._orderInfoList.Remove(oi);
            _presenter.DeleteOrderInfoControl(oi.OrderProductId);
            _presenter.RefreshControlStatus(oi.ControlName);
            LinkOrderInfo();
            LinkPhotoInfo();
        }

        private void tsbtnDeletePhoto_Click(object sender, EventArgs e)
        {
            int [] rowhander = gvPhotoInfo.GetSelectedRows();
            if (rowhander.Length>0)
            {
                PhotoInfo piToGetoi = gvPhotoInfo.GetRow(rowhander[0]) as PhotoInfo;

                OrderInfo oi = _orderInfoList.FirstOrDefault(o => o.OrderProductId == piToGetoi.OrderInfoId);
                for (int i = 0; i < rowhander.Length;i++ )
                {
                    PhotoInfo pi = gvPhotoInfo.GetRow(rowhander[i]) as PhotoInfo;
                    oi.PhotoList.Remove(pi);

                }
                _presenter.UpdatePhotoNumOnOrderInfoControl(oi.PhotoList.Count.ToString(), oi.OrderProductId);
                LinkOrderInfo();
                LinkPhotoInfo();
            }
            else
            {
                MessageBox.Show("请选择要删除的照片","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void tsbtnAddPhoto_Click(object sender, EventArgs e)
        {
            OrderInfo oi = gvOrderInfo.GetFocusedRow() as OrderInfo;

            if (DialogResult.OK == openFileDialog1.ShowDialog())
            {
                List<PhotoInfo> piList = _presenter.GetphotoInfoList(openFileDialog1.FileNames.ToList());
                foreach (var item in piList)
                {
                    item.OrderInfoId = oi.OrderProductId;
                    oi.PhotoList.Add(item);
                }
            }

            LinkOrderInfo();
            LinkPhotoInfo();
            
        }

        private void gvOrderInfo_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle > -1)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
            }
        }

        private void gvPhotoInfo_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle > -1)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
            }
        }


    }
}
